/** 
* All Rights Reserved , Copyright (C) 2020 , 青岛鼎信通讯股份有限公司
* 
* PmBudgetApplyService
* 项目预算申请单
* 
* 修改纪录
* 2020-05-22 版本：1.0 shenyouyi 创建。
* @version 版本：1.0
* @author 作者：shenyouyi
* 创建日期：2020-05-22
*/
package com.topscomm.pm.service.impl;

import com.topscomm.basic.BasicEntity;
import com.topscomm.basic.util.PrimaryKeyUtil;
import com.topscomm.pm.pojo.PmBudgetApplyEntity;
import com.topscomm.pm.service.IPmBudgetApplyService;
import com.topscomm.pm.service.IPmBudgetLineService;
import com.topscomm.pm.service.IPmProjectService;
import com.topscomm.pm.service.impl.auto.PmBudgetApplyServiceAuto;
import com.topscomm.pub.util.StringBufferProxy;
import com.topscomm.tap.common.TapSystemConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "pmBudgetApplyService")
@Scope(value = "singleton")
public class PmBudgetApplyService extends PmBudgetApplyServiceAuto implements IPmBudgetApplyService {

	@Autowired
	private IPmProjectService pmProjectService;
	@Autowired
	private IPmBudgetLineService pmBudgetLineService;

	/**
	 * @author: shenyouyi
	 * @date: 2020-05-22
	 * @param entity
	 * @description:单号生成
	 * @modify:
	 */
	@Override
	protected void beforeInsert(BasicEntity entity){		
		super.beforeInsert(entity);
		if (entity.getId() <= 0) {// 赋值Id和单号
			long[] arrayId = PrimaryKeyUtil.GeneIDs(entity.getTableName(), 1);
			entity.setId(arrayId[0]);
			String docNo = String.valueOf(arrayId[0]);
			docNo = docNo.substring(0, 6) + docNo.substring(9);
			entity.setValue(PmBudgetApplyEntity.FieldDocno, docNo);
		}
	}

	/**
	  * @author: shenyouyi
	  * @date: 2020/6/15
	  * @param 
	  * @description: 更新项目信息中的预算值
	  **/
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void updateBudgetMoneyOfProjectBase(String sourceId, Integer state) {
		PmBudgetApplyEntity pmBudgetApplyEntity = this.queryById(sourceId);
		StringBufferProxy sql = new StringBufferProxy();
		// 更新子表中的state
		Map<String, Object> updateFields = new HashMap<>();
		updateFields.put("state", state);
		sql.appendSingle("budgetapplyid = {0}", sourceId);
		pmBudgetLineService.updateByWhere(updateFields, sql.toString());
		// 计算预算总和
		sql.clear();
		sql.appendSingle(" state = {0} and projectid = {1}", TapSystemConst.TapApprovalBillStateEnum.Approved, pmBudgetApplyEntity.getProjectid());
		List<String> queryFields = new ArrayList<>();
//		queryFields.add("sum(budgetmoney) as totalmoney");
		queryFields.add("sum(NVL(budgetmoney,0)) as totalmoney");
		List<Map<String,Object>> budgetMoneyList = pmBudgetLineService.queryMapFieldsByWhere(sql.toString(), queryFields);
		BigDecimal totalMoney = BigDecimal.ZERO;
		if(budgetMoneyList.size()>0 && budgetMoneyList.get(0)!=null){
			totalMoney =  (BigDecimal) budgetMoneyList.get(0).get("totalmoney");
		}
		// 更新项目表中的预算值
		updateFields.clear();
		updateFields.put("budgetmoney", totalMoney);
		sql.clear();
		sql.appendSingle("id = {0}", pmBudgetApplyEntity.getProjectid());
		pmProjectService.updateByWhere(updateFields, sql.toString());
	}

}
